<?php
/**
 *UserServices
 * @author tan bing
 * @date 2021-08-10 14:27
 */


namespace Modules\User\Services\Admin;


use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Modules\User\Entities\User;
use Modules\User\Repositories\Contracts\UserInterface;
use Modules\User\Validate\Admin\CapitalValidate;
use Modules\User\Validate\Admin\UserValidate;

class UserServices extends BaseService
{
    /**
     * @var Request
     * @author tan bing
     */
    private $request;

    /**
     * @var UserValidate
     * @author tan bing
     */
    private $validate;

    /**
     * @var UserInterface
     * @author tan bing
     */
    private $userInterface;

    /**
     * UserServices constructor.
     *
     * @param Request $request
     * @param UserInterface $userInterface
     * @param UserValidate $userValidate
     * @author tan bing
     * @date 2021-08-10 15:02
     */
    public function __construct(Request $request, UserInterface $userInterface, UserValidate $userValidate)
    {
        $this->request = $request;
        $this->validate = $userValidate;
        $this->userInterface = $userInterface;
    }

    /**
     * @return array
     * @author tan bing
     * @date 2021-08-10 15:03
     */
    public function getPageData()
    {
        $param = $this->request->input();
        $query = $this->userInterface->getPageData($param, $this->perPage());
        $count = $query->total();
        $data  = $query->items();
        return compact('count', 'data');
    }

    /**
     * @return mixed
     * @throws \Exception
     * @author tan bing
     * @date 2021-08-10 15:03
     */
    public function addAndEditUserDetail()
    {
        $data = $this->request->post();
        if (!$this->validate->scene('edit')->check($data)) {
            throw new \Exception($this->validate->getError());
        }
        return $this->userInterface->addAndEditUserDetail($data);
    }

    /**
     *
     * @param null $id
     * @return mixed
     * @author tan bing
     * @date 2021-07-19 13:10
     */
    public function getUserDetail($id = null)
    {
        !$id && $id = $this->request->input('id', '');
        return $this->userInterface->findById($id);
    }

    /**
     * @return mixed
     * @author tan bing
     * @date 2021-06-24 9:59
     */
    public function deleteUser()
    {
        $id     = $this->request->get('id', '');
        $model = new User();
        return $model->deleteData($id);
    }

    /**
     * @return mixed
     * @author tan bing
     * @date 2021-06-24 10:00
     */
    public function batchDeleteUser()
    {
        $ids    = $this->request->get('ids', '');
        $model = new User();
        return $model->batchDeleteData($ids);
    }

    /**
     * @author tan bing
     * @date 2021-06-24 10:59
     */
    public function editStatus()
    {
        $param = $this->request->post();
        if (!$this->validate->scene('status')->check($param)) {
            throw new \Exception($this->validate->getError());
        }
        $model = new User();
        if (!$result = $model->editStatus($param)) throw new \Exception('操作失败');

        return $result;
    }

    /**
     * @return bool
     * @throws \Throwable
     * @author tan bing
     * @date 2021-08-11 17:43
     */
    public function editUserCapital()
    {
        $param           = $this->request->post();
        $admin           = Auth::guard('admin')->user();
        $capitalValidate = new CapitalValidate();
        if (!$capitalValidate->check($param)) {
            throw new \Exception($capitalValidate->getError());
        }
        if ($param['amount'] + 0 <= 0) throw new \Exception('金额必须大于0');

        try {
            DB::beginTransaction();
            $this->userInterface->editUserCapital($param);
            $param['admin_id']    = $admin->id;
            $param['operate_name']  = $admin->nickname;
            $param['description'] = '后台管理员操作资金：' . $param['description'];
            $this->userInterface->addUserCapitalLog($param);
            DB::commit();
        } catch (\Exception $e) {
            DB::rollBack();
            throw new \Exception($e->getMessage());
        }

        return true;
    }

    /**
     * 获取用户资金流水记录列表.
     *
     * @return array
     * @author tan bing
     * @date 2021-08-13 10:30
     */
    public function getUserCapitalLogPageData()
    {
        $param = $this->request->input();
        $query = $this->userInterface->getUserCapitalLogPageData($param, $this->perPage());
        $count = $query->total();
        $data  = $query->items();

        return compact('count', 'data');
    }

    /**
     * 获取用户操作日志记录列表.
     *
     * @return array
     * @author tan bing
     * @date 2021-08-13 10:30
     */
    public function getUserLogPageData()
    {
        $param = $this->request->input();
        $query = $this->userInterface->getUserLogPageData($param, $this->perPage());
        $count = $query->total();
        $data  = $query->items();

        return compact('count', 'data');
    }

}